Use @Type() from class-transformer with a discriminator option that maps the type field value to specific subtype classes. Pair with @ValidateNested() so class-validator runs on the instantiated subtype. class-transformer reads the discriminator property and instantiates the correct class before validation runs.
The discriminator.property field must exist in the incoming JSON to select the correct subtype.
@ValidateNested() must accompany the discriminator @Type() for class-validator to run on the subtype.
Each subtype class has its own class-validator decorators that are validated independently.
If the discriminator value does not match any subType, class-transformer returns a plain object.
keepDiscriminatorProperty: false removes the type field from the instantiated subtype object.